package org.jeecg.modules.appPermission;

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.appPermission.service.AppPermissionService;
import org.jeecg.modules.system.entity.SysPermission;
import org.jeecg.modules.system.entity.SysRole;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: app菜单权限
 * @author: zwf
 * @create: 2025-06-13 14:10
 **/
@Slf4j
@Tag(name = "用户权限")
@RequestMapping("/app/permission")
@RestController
public class AppPermissionApi {
    @Resource
    private AppPermissionService service;

    @Operation(summary = "初始化菜单")
    @GetMapping("/")
    private Result<JSONObject> initPermissionByToken() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        JSONObject result = new JSONObject();
        // 获取用户角色
        List<String> roleList = service.getUserRole(loginUser);
        // 获取用户菜单权限
        List<SysPermission> permissionList = service.getUserPermission(roleList);
        // 封装菜单数据（app）
        result = service.packagePermission(permissionList);

        return Result.OK(result);
    }
}
